﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using DAL;
using DTO;

namespace BLL
{
    public class StaffBLL
    {
        private static int staffID = 0;
        GaraDataContext DB = new GaraDataContext();

        public IQueryable SelectAll()
        {
            return DB.Staffs.Select(n => new
            {
                n.StaffID,
                Name=n.LastName + " "+n.FirstName,
                n.Address,
                n.Sex,
                n.DateOfBirth,
                n.Tel,
                n.Email,
                n.CMND,
                n.StartDate,
                n.FinishDate,
                n.ImageProfile,
                n.Status
            }
            ).OrderByDescending(n=>n.StaffID).AsQueryable();
        }
        public string Insert(StaffDTO staffDTO)
        {
            try
            {
                Staff staff = new Staff();
                staff.StaffID = (staffID++).ToString();
                staff.FirstName = staffDTO.FirstName;
                staff.LastName = staffDTO.LastName;
                staff.Address = staffDTO.Address;
                staff.Sex = staffDTO.Sex;
                staff.DateOfBirth = staffDTO.DateOfBirth;
                staff.Tel = staffDTO.Tel;
                staff.Email = staffDTO.Email;
                staff.CMND = staffDTO.CMND;
                staff.StartDate = staffDTO.StartDate;
                try
                {
                    Image picture = staffDTO.ImageProfile;
                    MemoryStream ms = new MemoryStream();
                    picture.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    staff.ImageProfile = ms.GetBuffer();
                }
                catch (Exception e)
                {
                }
                staff.Status = staffDTO.Status;
                DB.Staffs.InsertOnSubmit(staff);
                DB.SubmitChanges();
                return MessageAction.Success;

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string Update(StaffDTO staffDTO)
        {
            try
            {
                Staff staff = DB.Staffs.Where(s=>s.StaffID==staffDTO.StaffID).FirstOrDefault();
                staff.FirstName = staffDTO.FirstName;
                staff.LastName = staffDTO.LastName;
                staff.Address = staffDTO.Address;
                staff.Sex = staffDTO.Sex;
                staff.DateOfBirth = staffDTO.DateOfBirth;
                staff.Tel = staffDTO.Tel;
                staff.Email = staffDTO.Email;
                staff.CMND = staffDTO.CMND;
                staff.StartDate = staffDTO.StartDate;
                try
                {
                    Image picture = staffDTO.ImageProfile;
                    MemoryStream ms = new MemoryStream();
                    picture.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    staff.ImageProfile = ms.GetBuffer();
                }
                catch (Exception e)
                {
                }
                staff.Status = staffDTO.Status;
                DB.SubmitChanges();
                return MessageAction.Success;

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
       
        public IQueryable SearchIDName(string key)
        {
            int a = 5;

            if (key.Length == 4 && key.ToCharArray()[0].ToString().ToUpper().Equals("S") && MessageAction.IsNumber(key.Substring(1, 3)) && (Convert.ToInt32(key.Substring(1, 3)).GetType() == a.GetType()))
            {
                return DB.Staffs.Select(s => new
                {
                    StaffID = s.StaffID,
                    Name = s.LastName + " " + s.FirstName,
                    Address = s.Address,
                    Sex = s.Sex,
                    DateOfBirth = s.DateOfBirth,
                    Tel = s.Tel,
                    Email = s.Email,
                    CMND = s.CMND,
                    StartDate = s.StartDate,
                    FinishDate = s.FinishDate,
                    ImageProfile = s.ImageProfile,
                    Status = s.Status
                }).Where(s => s.StaffID == key).AsQueryable();
            }
            else
            {
                return DB.Staffs.Select(s => new
                {
                    StaffID = s.StaffID,
                    Name = s.LastName + " " + s.FirstName,
                    Address = s.Address,
                    Sex = s.Sex,
                    DateOfBirth = s.DateOfBirth,
                    Tel = s.Tel,
                    Email = s.Email,
                    CMND = s.CMND,
                    StartDate = s.StartDate,
                    FinishDate = s.FinishDate,
                    ImageProfile = s.ImageProfile,
                    Status = s.Status
                }).Where(s => s.Name.Contains(key)).AsQueryable();
            }
        }
    }
}
